<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="签订人：" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入签订人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item style="margin-left: 20px">
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery" v-has-permission="['ums:agreement_record:list']">搜索</el-button>
        <el-button icon="el-icon-refresh" type="danger" size="small" @click="resetQuery">重置</el-button>
        <el-button
          v-has-permission="['ums:agreement_record:delete']"
          type="danger"
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
        >删除
        </el-button>
        <el-button
          v-has-permission="['ums:agreement_record:export']"
          type="info"
          icon="el-icon-download"
          size="small"
          @click="handleExport"
        >导出
        </el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="agreement_recordList"
      border
      :header-cell-style="{background:'#EAE9E9FF'}"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <!--      <el-table-column label="昵称" align="center" prop="nickName" />-->
      <el-table-column label="类型" align="center" prop="remark" />
      <el-table-column label="签订时间" align="center" prop="createTime" />
      <el-table-column label="签订图片" align="center" prop="imgUrl">
        <template slot-scope="scope">
          <el-image :src="scope.row.imgUrl" style="width: 50px;height: 50px" :preview-src-list="[scope.row.imgUrl]">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline" />
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-has-permission="['ums:agreement_record:info']"
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >查看
          </el-button>
          <el-button
            v-has-permission="['ums:agreement_record:delete']"
            size="mini"
            icon="el-icon-delete"
            type="danger"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pagination.total > 0"
      :total="pagination.total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="handleQuery"
    />
    <!-- 添加或修改用户签署协议记录对话框 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="60%" append-to-body>
      <div id="printAgreement" style="padding: 10px 16px" v-html="form.agreement" />
      <div slot="footer" class="dialog-footer">
        <el-button v-print="printObj" type="primary">下载合同</el-button>
        <el-button type="primary" @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { downloadFile } from '@/utils'
import checkPermission from '@/utils/permission'
import {
  getAgreement_record,
  addAgreement_record,
  delAgreement_record,
  exportAgreement_record,
  listAgreement_record,
  updateAgreement_record
} from '@/api/ums/agreement'

export default {
  name: 'AgreementRecord',
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      pagination: {
        pageNum: 1,
        pageSize: 10,
        total: 0
      },
      // 用户签署协议记录表格数据
      agreement_recordList: [],
      // 弹出层
      dialog: {
        title: undefined,
        visible: false,
        type: undefined // type 操作类型：1-新增 2-修改
      },
      // 查询参数
      queryParams: {
        username: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      printObj: {
        id: 'printAgreement',
        popTitle: '悦舒美',
        beforeOpenCallback(vue) {
          console.log('打开之前')
        },
        openCallback(vue) {
          console.log('执行了打印')
        },
        closeCallback(vue) {
          console.log('关闭了打印工具')
        }
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    /** 查询用户签署协议记录列表 */
    getList() {
      this.loading = true
      Object.keys(this.queryParams).forEach(key => {
        this.queryParams[key] == '' ? this.queryParams[key] = null : false
      })
      this.queryParams.pageNum = this.pagination.pageNum
      this.queryParams.pageSize = this.pagination.pageSize
      listAgreement_record(this.queryParams).then(response => {
        const { data } = response
        this.agreement_recordList = data.list
        this.pagination.total = data.totalCount
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.reset()
      this.closeDialog()
    },
    handleReset() {
      this.pagination = {
        pageNum: 1,
        pageSize: 10,
        total: 0
      }
      this.queryParams = {
        uid: null,
        username: null,
        levelId: null,
        imgUrl: null
      }
      this.handleQuery()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        uid: null,
        username: null,
        levelId: null,
        imgUrl: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        isDelete: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    resetForm(val) {
      if (this.$refs[val]) {
        this.$refs[val].resetFields()
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.dialog = {
        title: '添加用户签署协议记录',
        visible: true,
        type: 'add'
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.dialog = {
        title: '用户签署协议',
        visible: true,
        type: 'edit'
      }
      const id = row.id || this.ids
      getAgreement_record(id).then(response => {
        this.form = response.data
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.dialog.type === 'edit') {
            updateAgreement_record(this.form).then(response => {
              this.$message.success('修改成功')
              this.closeDialog()
              this.getList()
            })
          } else {
            addAgreement_record(this.form).then(response => {
              this.$message.success('新增成功')
              this.closeDialog()
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$confirm('是否确认删除用户签署协议记录编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delAgreement_record(ids)
      }).then(() => {
        this.getList()
        this.$message.success('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      delete queryParams.pageNum
      delete queryParams.pageSize
      this.$confirm('是否确认导出所有用户签署协议记录数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportAgreement_record(queryParams)
      }).then(response => {
        downloadFile(response.data, 'client', 'xls')
      })
    },
    closeDialog() {
      this.dialog = {
        title: undefined,
        visible: false,
        type: undefined
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
